package com.ruoyi.activity.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.activity.domain.Activity;
import com.ruoyi.activity.mapper.ActivityTypeMapper;
import com.ruoyi.activity.domain.ActivityType;
import com.ruoyi.activity.service.IActivityTypeService;

/**
 * 活动类型Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-27
 */
@Service
public class ActivityTypeServiceImpl implements IActivityTypeService 
{
    @Autowired
    private ActivityTypeMapper activityTypeMapper;

    /**
     * 查询活动类型
     * 
     * @param id 活动类型主键
     * @return 活动类型
     */
    @Override
    public ActivityType selectActivityTypeById(Long id)
    {
        return activityTypeMapper.selectActivityTypeById(id);
    }

    /**
     * 查询活动类型列表
     * 
     * @param activityType 活动类型
     * @return 活动类型
     */
    @Override
    public List<ActivityType> selectActivityTypeList(ActivityType activityType)
    {
        return activityTypeMapper.selectActivityTypeList(activityType);
    }

    /**
     * 新增活动类型
     * 
     * @param activityType 活动类型
     * @return 结果
     */
    @Transactional
    @Override
    public int insertActivityType(ActivityType activityType)
    {
        activityType.setCreateTime(DateUtils.getNowDate());
        int rows = activityTypeMapper.insertActivityType(activityType);
        insertActivity(activityType);
        return rows;
    }

    /**
     * 修改活动类型
     * 
     * @param activityType 活动类型
     * @return 结果
     */
    @Transactional
    @Override
    public int updateActivityType(ActivityType activityType)
    {
        activityTypeMapper.deleteActivityByType(activityType.getId());
        insertActivity(activityType);
        return activityTypeMapper.updateActivityType(activityType);
    }

    /**
     * 批量删除活动类型
     * 
     * @param ids 需要删除的活动类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteActivityTypeByIds(Long[] ids)
    {
        activityTypeMapper.deleteActivityByTypes(ids);
        return activityTypeMapper.deleteActivityTypeByIds(ids);
    }

    /**
     * 删除活动类型信息
     * 
     * @param id 活动类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteActivityTypeById(Long id)
    {
        activityTypeMapper.deleteActivityByType(id);
        return activityTypeMapper.deleteActivityTypeById(id);
    }

    /**
     * 新增活动纪实信息
     * 
     * @param activityType 活动类型对象
     */
    public void insertActivity(ActivityType activityType)
    {
        List<Activity> activityList = activityType.getActivityList();
        Long id = activityType.getId();
        if (StringUtils.isNotNull(activityList))
        {
            List<Activity> list = new ArrayList<Activity>();
            for (Activity activity : activityList)
            {
                activity.setType(id);
                list.add(activity);
            }
            if (list.size() > 0)
            {
                activityTypeMapper.batchActivity(list);
            }
        }
    }
}
